package com.sprint.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderNacosController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${server.port}")
    private String serverport;

    @Value("${service-url.nacos-user-service}")
    private String serviceURL;

    @GetMapping(value = "/consumer/nacos/payment/{id}")
    public String getName(@PathVariable Integer id){
        //Access through the combination of LoadBalanceClient and RestTemplate
//        System.out.println("--------------start-0-------------------");
//        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-payment-provider");
//        System.out.println("--------------start-1-------------------" + serviceInstance.toString());
//        String path = String.format("http://%s:%s/nacos/get/%s",serviceInstance.getHost() ,serviceInstance.getPort(),id);

        String path = serviceURL + "/nacos/payment/" + id;
        System.out.println("request path:" + path );
        return restTemplate.getForObject(serviceURL + "/nacos/payment/" + id, String.class);
    }
}
